package com.lzw.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzw.anno.Log;
import com.lzw.pojo.MaintenanceWorker;
import com.lzw.service.admin.MaintenanceWorkerManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/maintenanceWorkerManagement")
@CrossOrigin(origins = "http://localhost:7070")
public class MaintenanceWorkerManagementController {

    @Autowired
    private MaintenanceWorkerManagementService maintenanceWorkerManagementService;

    //分页查询维修员信息
    @GetMapping("/list")
    public PageInfo<MaintenanceWorker> listMaintenanceWorker(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name, String phone){
        PageHelper.startPage(pageNum,pageSize);
        List<MaintenanceWorker> list = maintenanceWorkerManagementService.listMaintenanceWorker(pageNum, pageSize, name, phone);
        return new PageInfo<>(list);
    }

    //添加维修员
    @Log
    @PostMapping("/add")
    public ResponseEntity<String> addMaintenanceWorker(@RequestBody MaintenanceWorker maintenanceWorker){
        try {
            maintenanceWorkerManagementService.addMaintenanceWorker(maintenanceWorker);
            return ResponseEntity.ok("添加维修员成功");
        } catch (IllegalArgumentException e){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    //删除维修员
    @Log
    @DeleteMapping("/delete/{ids}")
    public String deleteMaintenanceWorker(@PathVariable List<Integer> ids){
        maintenanceWorkerManagementService.deleteMaintenanceWorker(ids);
        return "删除维修员成功";
    }

    //更新维修员信息
    @Log
    @PutMapping("/update")
    public ResponseEntity<String> updateMaintenanceWorker(@RequestBody MaintenanceWorker maintenanceWorker){
        try {
            maintenanceWorkerManagementService.updateMaintenanceWorker(maintenanceWorker);
            return ResponseEntity.ok("更新维修员成功");
        } catch (IllegalArgumentException e){
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
        }
    }

    //根据id查询维修员信息
    @GetMapping("/get/{id}")
    public MaintenanceWorker getMaintenanceWorkerById(@PathVariable("id") int id){
        return maintenanceWorkerManagementService.getMaintenanceWorkerById(id);
    }
}
